var urlHyperlink = {
    version: "0.5.1",
    init: function() {
        var appcontent = document.getElementById("appcontent");
        appcontent.addEventListener("DOMContentLoaded", urlHyperlink.load, true);
    },
    load: function(e) {
        if (e.originalTarget.nodeName == "#document") {
            var d = window.content.document;
            var o = [o || d.documentElement || d.body];
            var r = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi;

            var i = 0;
            var n = 0;
            var j, k, l, m, t;

            do {
                m = o[i];
                if (m.nodeType === 3 && m.parentNode.nodeName != 'A' && m.parentNode.nodeName != 'STYLE' && m.parentNode.nodeName != 'SCRIPT' && m.parentNode.nodeName != 'META' && m.parentNode.nodeName != 'LINK' && m.parentNode.nodeName != 'TITLE') {
                    r.lastIndex = 0;
                    l = r.exec(m.nodeValue);
                    if (l !== null) {
                        k = l[0].length;
                        if (r.lastIndex > k) {
                            m.splitText(r.lastIndex - k);
                            m = m.nextSibling;
                        }
                        if (m.nodeValue.length > k) {
                            m.splitText(k);
                            o[i++] = m.nextSibling;
                        }

                        t = d.createElement('a');
                        t.href = l[0];

                        t.appendChild(d.createTextNode(l[0])); n++;
                        m.parentNode.replaceChild(t, m);
                    }
                } else {
                    j = m.childNodes.length;
                    while (j) { o[i++] = m.childNodes.item(--j); }
                }
            } while (i--);
            return n;
        }
    },
    log: function(msg) {
        var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
        consoleService.logStringMessage(msg);
    }
}

window.addEventListener("load", function() { urlHyperlink.init(); }, false);
